<?php

/**
 * Created by PhpStorm.
 * Author: ls
 * Time: 2023/11/7 10:11
 * Desc: 秒杀
 */
class Model_Api_V2_Marketing_Strategy_Seckill extends Contract_Api_Marketing_Strategy
{
    private static $instance = null;

    private $application_name = '秒杀中';

    private $application_code = 'seckill';

    private $icon = '&#xe63e;';

    // 包含的产品id数组
    private static $product_ids = array();

    public function __construct($type_id)
    {

    }

    /**
     * @param int $type_id
     * @param array $params
     * @return Model_Api_V2_Marketing_Strategy_Seckill|null
     */
    public static function get_instance(int $type_id, array $params = [])
    {
        // 应用是否安装
        if (!St_Functions::is_normal_app_install('seckill')) {
            return null;
        }

        // 产品类型没有拼团不做后续产品拼团判断
        $now_time = time();
        $data     = DB::select('product_id')
            ->from('seckill')
            ->where('type_id', '=', $type_id)
            ->and_where('start_date', '<', $now_time)
            ->and_where('end_date', '>', $now_time)
            ->execute()
            ->as_array();
        if (empty($data)) {
            return null;
        }

        self::$product_ids = array_column($data, 'product_id');

        if (is_null(self::$instance)) {
            self::$instance = new self($type_id);
        }

        return self::$instance;
    }

    public function is_support($product_id)
    {
        $product_ids = self::$product_ids;
        if (!in_array($product_id, $product_ids)) {
            return false;
        }

        return array(
            'code' => $this->application_code,
            'name' => $this->application_name,
            'icon' => $this->icon
        );
    }
}